package gw.com.android.ui.chart;

import com.gwtsz.chart.output.utils.ChartConfig;
import com.gwtsz.chart.output.utils.GTTIndexDataRequest;
import com.gwtsz.chart.output.utils.GTTIndicatorsModel;
import com.gwtsz.chart.output.utils.GTTKDataManager;
import com.gwtsz.chart.output.utils.GTTKDataModel;
import com.gwtsz.chart.output.utils.GTTKDataRequest;
import com.gwtsz.chart.output.utils.GTTTimeDataModel;

import gw.com.android.terminal.AppTerminal;
import www.com.library.app.Logger;

/**
 * Created by aries.zhan on 2016/11/8.
 */
public class ReqDataUtils {

    /**
     * 请求k线条数
     */
    public static final int nNum = -400;
    private static ReqDataUtils instance = null;

    /**
     * 请求状态,是否已经是最新数据
     */
    public boolean isReqLastest = false;

    public static ReqDataUtils instance() {
        if (instance == null) {
            instance = new ReqDataUtils();
        }
        return instance;
    }

    /**
     * 请求k线数据
     *
     * @param mUiCode   产品uicode
     * @param zoneType  板块
     * @param kLineType
     */
    public void reqKLineData(int mUiCode, int zoneType, int kLineType, int uStartTime) {
        synchronized (instance) {
            GTTKDataRequest mRequest = new GTTKDataRequest();
            mRequest.uBourseID = zoneType;
            mRequest.uCodeID = mUiCode;
            mRequest.uStartTime = uStartTime;
            mRequest.uKLineType = kLineType;
            mRequest.uParam = 0;
            mRequest.nNum = nNum;
            Logger.e(" mRequest.uKLineType = " + mRequest.uKLineType + ",uStartTime=" + uStartTime);
            AppTerminal.instance().requestKData(mRequest);
        }
    }

    /**
     * 请求指标数据
     */
    public GTTIndexDataRequest reqIndictorData(int mUiCode, int num, int uStartTime, int uFromHead) {
        return reqIndictorParam(mUiCode, num, uStartTime, uFromHead);
    }

    /**
     *
     */
    public void copyIndictorData(GTTIndexDataRequest mRequest) {
        /**
         * 复制数据到现在的对象中
         */
        if (null == GTTKDataManager.instance().mSuperIndicators) {
            GTTKDataManager.instance().mSuperIndicators = new GTTIndicatorsModel();
        }
        if (null == GTTKDataManager.instance().mSecondIndicators) {
            GTTKDataManager.instance().mSecondIndicators = new GTTIndicatorsModel();
        }
        if (null != mRequest && null != mRequest.superIndic) {
            GTTKDataManager.instance().mSuperIndicators.copyIndicator(mRequest.superIndic);
        }
        if (null != mRequest && null != mRequest.secondIndic) {
            GTTKDataManager.instance().mSecondIndicators.copyIndicator(mRequest.secondIndic);
        }
    }

    /**
     * 请求最后一根k线的指标值
     *
     * @param isAdd 标识是更新最后一个指标值还是添加一个指标值
     */
    private void reqLastIndictorData(boolean isAdd, GTTIndexDataRequest mRequest) {

        /**
         * 复制数据到现在的对象中
         */
        if (mRequest.uKLineType == ChartConfig.instance().getChartPeriodicity().nativeKLineType()) {
            GTTKDataManager.instance().mSuperIndicators.copyLastIndicator(isAdd, mRequest.superIndic);
            GTTKDataManager.instance().mSecondIndicators.copyLastIndicator(isAdd, mRequest.secondIndic);
            //int size=GTTKDataManager.instance().mSecondIndicators.mLineList.get(GTTKDataManager.instance().mSecondIndicators.mLineList.size()-1).mLineData.getmLines().size();
            //Logger.e("spenceSecondIndic","value="+GTTKDataManager.instance().mSecondIndicators.mLineList.get(GTTKDataManager.instance().mSecondIndicators.mLineList.size()-1).mLineData.getmLines().get(size-1));
        }

    }

    /**
     * 设置指标数据获取的参数
     *
     * @param mUiCode
     * @param num
     * @param uStartTime
     */
    private GTTIndexDataRequest reqIndictorParam(int mUiCode, int num, int uStartTime, int uFromHead) {
        GTTIndexDataRequest mRequest = new GTTIndexDataRequest();
        mRequest.superIndic = new GTTIndicatorsModel();
        mRequest.secondIndic = new GTTIndicatorsModel();
        mRequest.superName = ChartConfig.instance().getMainKType();
        mRequest.secondName = ChartConfig.instance().getSecondKType();
        mRequest.uCodeID = mUiCode;
        mRequest.nNum = num;
        mRequest.uStartTime = uStartTime;
        mRequest.uFromHead = uFromHead;
        mRequest.uKLineType = ChartConfig.instance().getChartPeriodicity().nativeKLineType();
//        Logger.e(" mRequest.uKLineType = " + mRequest.uKLineType + ", uStartTime=" + uStartTime +" , mRequest.nNum = " + mRequest.nNum);
        AppTerminal.instance().getKIndicatorsDataList(mRequest);

        return mRequest;
    }

    /**
     * 根据报价刷新或者添加最新一根k线
     */
    public int updataOrAddLastLine(GTTKDataModel kDataModel2, GTTIndexDataRequest mRequest) {
        if (null != kDataModel2) {
            GTTKDataModel kDataModel = GTTKDataManager.instance().getLastKLineModel();
            if (kDataModel != null && kDataModel2 != null) {
                if (kDataModel.priceTime == kDataModel2.priceTime) {
                    GTTKDataManager.instance().setLastKLineData(kDataModel2);
                    reqLastIndictorData(false, mRequest);
                } else if (kDataModel.priceTime < kDataModel2.priceTime) {
                    GTTKDataManager.instance().addKLineData(kDataModel2);
                    reqLastIndictorData(true, mRequest);
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     * 处理分时最后一根线数据
     *
     * @param mRound
     * @return
     */
    public int updateLastTimeLine(GTTTimeDataModel mRound) {
        int rangeMinute = (int) ((mRound.mTime - GTTKDataManager.instance().lastTime) / 60);
        if (rangeMinute > 0
                && GTTKDataManager.instance().mTimeDataList != null
                && GTTKDataManager.instance().mTimeDataList.size() < 1441) {
            GTTKDataManager.instance().addTimeModel(mRound);
        } else {
            GTTKDataManager.instance().setLastTimeData(mRound);
        }
        GTTKDataManager.instance().lastTime = mRound.mTime;
        return 0;
    }
}
